<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择0-龙钞 1-蛇钞" clearable>
          <el-option
            v-for="dict in dict.type.money_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="冠字号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入冠字号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="numberType">
        <el-select v-model="queryParams.numberType" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.number_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评分" prop="nakedOrGraded">

        <el-select v-model="queryParams.nakedOrGraded" placeholder="请选择" clearable>
          <el-option
            v-for="(item,index) in nakedOrGradeds"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="得分" prop="gradedScore">
        <el-select v-model="queryParams.gradedScore" clearable placeholder="">
          <el-option
            v-for="dict in ['65','66','67','67+','68','69','70']"
            :key="dict"
            :label="dict"
            :value="parseInt(dict)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="微信号" prop="contactWechatId">
        <el-input
          v-model="queryParams.contactWechatId"
          placeholder="请输入联系人微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="numberOwnerAccount">
        <el-input
          v-model="queryParams.numberOwnerAccount"
          placeholder="请输入联系人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:record:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:record:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          class="upload-demo"
          action="https://peihao0158.com:54430/system/moneyRecord/import"
          :on-success="handleImportSuccess"
          :on-error="handleImportError"
          :before-upload="beforeUpload"
          :headers = "headers"
          :show-file-list="false"
          accept=".xls,.xlsx"
        >
          <el-button type="primary" plain icon="el-icon-upload" size="mini">导入</el-button>
        </el-upload>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="400px" v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="类型" align="center" prop="billType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.money_type" :value="scope.row.billType"/>
        </template>
      </el-table-column>
      <el-table-column label="冠字号" align="center" prop="serialNumber" />
      <el-table-column label="类别" align="center" prop="numberType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.number_type" :value="scope.row.numberType"/>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="nakedOrGraded">
        <template slot-scope="scope">
          <span v-if="scope.row.nakedOrGraded === 0">裸币</span>
          <span v-if="scope.row.nakedOrGraded === 1">ACG</span>
          <span v-if="scope.row.nakedOrGraded === 2">PMG</span>
          <span v-if="scope.row.nakedOrGraded === 3">PCGS</span>
        </template>
      </el-table-column>
      <el-table-column label="得分" align="center" prop="gradedScore">
        <template slot-scope="scope">
          {{scope.row.gradedScore}}
        </template>
      </el-table-column>
      <el-table-column label="联系人微信号" align="center" prop="contactWechatId" />
      <el-table-column label="号码归属人账号" align="center" prop="numberOwnerAccount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <!-- 添加或修改冠字号记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="类型" prop="billType">
          <el-select :disabled="true" v-model="form.billType" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.money_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="冠字号" prop="serialNumber">
          <el-input :disabled="true" v-model="form.serialNumber" placeholder="请输入冠字号" />
        </el-form-item>
        <el-form-item label="类别" prop="numberType">
          <el-select :disabled="true" v-model="form.numberType" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.number_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评分" prop="nakedOrGraded">
          <el-select v-model="form.nakedOrGraded" placeholder="请选择" clearable>
            <el-option
              v-for="(item,index) in nakedOrGradeds"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="得分" prop="gradedScore">
          <el-select v-model="form.gradedScore" placeholder="请选择">
            <el-option
              v-for="dict in ['65','66','67','67+','68','69','70']"
              :key="dict"
              :label="dict"
              :value="parseInt(dict)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="creatorAccount">
          <el-input v-model="form.creatorAccount" placeholder="请输入创建人账号" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactWechatId">
          <el-input v-model="form.contactWechatId" placeholder="请输入联系人微信号" />
        </el-form-item>
        <el-form-item label="号码归属人" prop="numberOwnerAccount">
          <el-input v-model="form.numberOwnerAccount" placeholder="请输入号码归属人账号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record";
import Cookies from "js-cookie";

export default {
  name: "Record",
  dicts: ['number_type', 'money_type'],
  data() {
    return {
      // 0-裸 1-ACG 2-PMG 3-PCGS
      nakedOrGradeds:[
        {value:0,name:'裸币'},
        {value:1,name:'ACG'},
        {value:2,name:'PMG'},
        {value:3,name:'PCGS'}
      ],
      headers:{
        Authorization: "Bearer " + Cookies.get("Admin-Token")
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 冠字号记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billType: null,
        serialNumber: null,
        numberType: null,
        nakedOrGraded: null,
        gradedScore: null,
        creatorAccount: null,
        contactWechatId: null,
        isNiceNumber: null,
        numberOwnerAccount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询冠字号记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billType: null,
        serialNumber: null,
        numberType: null,
        nakedOrGraded: null,
        gradedScore: null,
        creatorAccount: null,
        createTime: null,
        contactWechatId: null,
        isNiceNumber: null,
        numberOwnerAccount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let arr = []
      selection.forEach(item => {
        item.ids.forEach(id =>{
          arr.push(id)
        })
      })
      this.ids = arr
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加冠字号记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改冠字号记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.ids || this.ids;
      this.$modal.confirm('是否删除？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/moneyRecord/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    },
    // 导入成功回调
    handleImportSuccess(response, file) {
      if (response.code == 500){
        this.$modal.msgError(response.msg);
      }else {
        this.$modal.msgSuccess("导入成功");
        this.getList();
      }

    },
    // 导入失败回调
    handleImportError() {
      this.$modal.msgError("导入失败，请检查文件格式或联系管理员");
    },
    // 上传前校验，只允许上传 Excel 文件
    beforeUpload(file) {
      const isExcel = file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
      if (!isExcel) {
        this.$message.error('只能上传 Excel 文件');
      }
      return isExcel;
    }
  }
};
</script>
